import SiderBav from '@/components/sider-nav/index'
export default {
  name: 'planList',
  components: {
    SiderBav
  },
  data() {
    return {
      currentPage:1,
      planNav: [{
        icon: "",
        text: '添加计划',
        router: '/add_plan'
      },
      {
        icon: "",
        text: '移除计划',
        router: '',
        fn(){
          this.$parent.delet();
        }
      },
      {
        icon: "",
        text: '修改状态',
        router: '',
        child:[
          {
            icon: "",
            text: '正在投放',
            router: '',
            fn:function(){
              this.$parent.changestatus(1);
            }
          },
          {
            icon: "",
            text: '暂停投放',
            router: '',
            fn:function(){
              this.$parent.changestatus(0);
            }
          },
          {
            icon: "",
            text: '投放结束',
            router: '',
            fn:function(){
              this.$parent.changestatus(9);
            }
          }
        ]
      }
    ],
      options: [{
          value: '',
          label: '全部状态'
        },
        {
          value: '0',
          label: '暂停'
        },
        {
          value: '1',
          label: '投放中'
        },
        {
          value: '9',
          label: '投放结束'
        }
      ],
      planTypeList: [{

          value: '',
          label: '全部计划类型'
        },
        {
          value: '-1',
          label: '自定义计划'
        },
        {
          value: '1',
          label: '日常托管计划'
        },
        {
          value: '2',
          label: '日常推荐计划'
        },
        {
          value: '3',
          label: '拉新托管计划'
        },
        {
          value: '4',
          label: '拉新推荐计划'
        }
      ],
      payTypeList: [{
          value: '',
          label: '所有付费方式'
        },
        {
          value: '2',
          label: '按展现付费（CPM）'
        },
        {
          value: '8',
          label: '按点击付费（CPC）'
        }
      ],
      planStatus: '1',
      planType: '',
      payType: '',
      dataList: [],
      total_count: 0,
      searchPlanName: "",
      searchOptions: {},
      checkList:[],
      checkListAll:false,
      source:null,
      loading:false,
      sortType:'',
      sort:undefined,
      order:undefined
    }
  },
  // computed: {
  //   planSidebar() {
  //     return this.$store.state.planSidebar
  //   }
  // },
  watch: {
    planStatus(val) {
      this.search();
    },
    planType(val) {
      this.search();
    },
    payType(val) {
      this.search();
    },
    searchPlanName() {
      this.search();
    },
    checkListAll(val){
        this.checkList=[];
        if(val){
          this.dataList.forEach(element => {
                this.checkList.push(element.id);
          });
        }
    }
  },
  mounted() {
    this.search();
  },
  filters: {
    planStatus: function (n) {
      return {
        0: "暂停",
        1: '投放中',
        9: '投放结束'
      } [n];
    }
  },
  methods: {
    pagechange(val) {
      this.search(val);
    },
    search(page) {
      let page_num = page || 1;
      let params = {
        page_num: page_num,
        page_size: 20,
        sort:this.sort,
        order:this.order
      };
      params = Object.assign({}, params, this.searchOptionsfn().params);
      this.dataList = [];
			if(typeof this.source=='function'){
             this.source();//取消请求
      }
      this.loading=true;
      const loading = this.$loading({
        lock: true,
        text: '数据获取中....',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
        customClass:'lodding'
      });
      let _this=this;
      this.currentPage=page_num;
      this.$http({
          method: 'get',
          url: this.searchOptions.url,
          params: params,
          cancelToken: new this.$http.CancelToken(c=>{
            this.source = c;
          })
        }).then((data) => {
          this.loading=false;
          loading.close();
          if (!this.$http.interceptors(this, data)) return;

          for (let i = 0, o; o = data.data.data.list[i++];) {
            o['hover'] = false;
          }
          this.dataList = data.data.data.list;
          this.total_count = data.data.data.total_count;
        })
        .catch(function (error) {
          _this.loading=false; 
          loading.close();
          console.log(error);
        });
    },
    trhoverFn(o) {
      for (let i = 0, o; o = this.dataList[i++];) {
        o['hover'] = false;
      }
      o.hover = true;
    },
    delet() {
      if(this.checkList.length<=0){
            this.$message.error('未选择计划');
            return false;
        }
        
        this.$confirm('确定移除计划?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            method: 'post',
            url: '/zuanzhan/campaign/del',
            data: this.checkList
          }).then((data) => {
            if (!this.$http.interceptors(this, data)) return;
            this.search();
          })
          .catch(function (error) {
            console.log(error);
          });
        }).catch(() => { 
                 
        });
    },
    searchOptionsfn(){
      this.searchOptions = {};
      this.searchOptions.url = '/zuanzhan/campaigns';
      this.searchOptions.params = {
        name: this.searchPlanName,
        online_status: this.planStatus,
        type: this.payType,
        marketingdemand: this.planType
      }
      return this.searchOptions;
    },
    changestatus(status){
        if(this.checkList.length<=0){
            this.$message.error('未选择计划');
            return false;
        }
        this.$confirm('确定修改计划状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           this.$http({
             method: 'post',
             url: '/zuanzhan/campaigns/status',
             data:{
               campaign_id_list:this.checkList,
               status:status+''
             },
           }).then((data) => {
               if (!this.$http.interceptors(this, data)) return;
               this.$message({
                 type: 'success',
                 message: '修改成功!'
               });
               this.search();
           })
           .catch(function (error) {
             console.log(error);
           });
        }).catch(() => {         
        });
    },
    sortfn(key){
        this.sortType=key;
        this.sort=key.split('-')[0];
        this.order=key.split('-')[1];
        this.search();
    },
    toCrowd(obj){
        this.$router.push({ path:'crowdsList', query:{id:obj.id,name:obj.name,day_budget:obj.day_budget}})
    },
    toAdgroup(obj){
        this.$router.push({ path:'adgroupList', query:{id:obj.id,name:obj.name,day_budget:obj.day_budget}})
    }
  }
}